package cn.zzzhy.spider;

import cn.zzzhy.entity.URL;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author zzzhy
 * @since 16/2/27 下午7:52
 */
public class SpiderPool {

    private static final int DEFAULT_POOL_SIZE = 1;
    private final int size;
    private final List<PageSpider> spiders = new ArrayList<>();
    public static final ConcurrentMap<String,URL> visitedURLs = new ConcurrentHashMap<>();
    public static final Set<String> remaining = new HashSet<>();
    public static final Set<String> all = new HashSet<>();

    public SpiderPool(String destUrl) {
        this(destUrl,DEFAULT_POOL_SIZE);
    }

    public SpiderPool(String destUrl, Integer size) {
        this.size = size == null || size < 0 ? DEFAULT_POOL_SIZE : size;
        initPool();
        remaining.add(destUrl);
        all.add(destUrl);
    }

    private void initPool() {
        PageSpider pageSpider;
        for (int i = 0; i < size; i++) {
            pageSpider = new PageSpider();
            spiders.add(pageSpider);
        }
    }

    public void startFetchUrls() {
        spiders.forEach(PageSpider::fetch);
    }

    public void stop() {
        spiders.forEach(PageSpider::stop);
    }

    public void pause() {
        spiders.forEach(PageSpider::pause);
    }
}
